/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');
    app.useModule("ui.table");
    var toastr =require('toastr');
    var datepicker = require('datepicker');
    app.controller('firstCtrl', ['$scope','$http','$filter',function ($scope, $http,$filter) {
        datepicker($scope);
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.item={};
        $scope.ctrlScope.first={};
        $scope.ctrlScope.first1={};
        $scope.ctrlScope.repay={};

        $scope.orderParams = {
            url: "/admin/item/getList",
            ajax:{item:'1'}
        };

        $scope.getHours= function () {
            var hours=[];
            for(var i=0;i<24;i++){
                if(i<10){
                    i="0"+i
                }
                hours.push(i)
            }
            return hours;
        };
        $scope.getMins= function () {
            var mins=[];
            for(var i=0;i<60;i++){
                if(i<10){
                    i="0"+i
                }
                mins.push(i)
            }
            return mins;
        };

        //项目统计
        $scope.countItem=function(param){
            $http({
                method: 'POST',
                url:"/admin/item/getCount",
                data:param
            }).success(function (data){
                $scope.ctrlScope.count=data;
            }).error(function(data){

            });
        };
        $scope.countItem();

        //搜索
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.item=1;
            $scope.ctrlScope.itemsearch.beginDate=$scope.ctrlScope.itemsearch.beginDateX;
            $scope.ctrlScope.itemsearch.endDate=$scope.ctrlScope.itemsearch.endDateX;
            $scope.ctrlScope.itemsearch.beginDate=$filter('date')($scope.ctrlScope.itemsearch.beginDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDate=$filter('date')($scope.ctrlScope.itemsearch.endDate,'yyyy-MM-dd');
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            $scope.orderParams.reload();
        };

        //搜索重置
        $scope.reset=function(){
            //$scope.orderParams.ajaxParam={item:1};
            //$scope.reload();
            $scope.ctrlScope.itemsearch = {};
            $scope.search();
            $scope.countItem();
        };


        //初审
        $scope.openViewModal=function(id){
            $scope.viewItemModal = !$scope.viewItemModal;
            $scope.getModalItem(id);
            $scope.ctrlScope.itemId=id;
        };

        //查询项目信息
        $scope.getModalItem = function(id){
            $scope.ctrlScope.item={};
            $scope.pics=[];
            $scope.ctrlScope.first={};
            $scope.ctrlScope.first1={};
            $http({
                method: 'POST',
                url: "/admin/item/getOneById",
                data: {
                    id: id
                }
            }).success(function(data) {
                console.log(data)
                $scope.ctrlScope.item= data.item;
                $scope.ctrlScope.item.loanUser=data.loanUser;
                $scope.ctrlScope.item.guaranteeUser=data.guaranteeUser;


                if(data.item.itemSingleMinInvestment==0){
                    $scope.ctrlScope.item.itemSingleMinInvestment="不设起投";
                }else{
                    $scope.ctrlScope.item.itemSingleMinInvestment=data.item.itemSingleMinInvestment;
                }
                if(data.item.itemMaxInvestment==0){
                    $scope.ctrlScope.item.itemMaxInvestment="不设上限";
                }else{
                    $scope.ctrlScope.item.itemMaxInvestment=data.item.itemMaxInvestment;
                }

                if(data.item.releaseTime===null){
                    $scope.ctrlScope.item.releaseTime='非定时标';
                }else{
                    $scope.ctrlScope.item.releaseTime=$filter('date')(data.item.releaseTime, 'yyyy-MM-dd HH:mm:ss')
                }

                $scope.ctrlScope.time= data.time;

                if($scope.ctrlScope.item.prepayment==1) {
                    if (data.itemPrepayment.partialPrepayment == 1) {
                        $scope.ctrlScope.item.partialPrepayment = 1
                    } else {
                        $scope.ctrlScope.item.partialPrepayment = 0
                    }
                    if (data.itemPrepayment.allPrepayment == 1) {
                        $scope.ctrlScope.item.allPrepayment = 1
                    } else {
                        $scope.ctrlScope.item.allPrepayment = 0
                    }
                }else{

                }

                if(data.itemPrepayment){
                    if(data.itemPrepayment.repaymentAudit==0){
                        $scope.ctrlScope.repay.repaymentAudit1=false;

                    }else{
                        $scope.ctrlScope.repay.repaymentAudit1=true;
                        $scope.ctrlScope.item.repaymentAudit=data.itemPrepayment.repaymentAudit;
                    }
                }
                $scope.imgs=[];
                $scope.personalImgs=[];
                $scope.carImgs=[];
                $scope.procedureImgs=[];
                $scope.riskImgs=[];
                $scope.skillImgs=[];
                for(var i=0;i<data.pictureList.length;i++){
                    if(data.pictureList[i].itemPictureType==1){
                        $scope.personalImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==2){
                        $scope.carImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==3){
                        $scope.procedureImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==4){
                        $scope.riskImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==5){
                        $scope.skillImgs.push(data.pictureList[i]);
                    }
                }
            }).error(function(data) {
            });
        };


        //初审提交
        $scope.submitForm = function(isValid){
            if (!isValid) {
                toastr.error("信息填写有误");
                return;
            }
            var datajson=$scope.ctrlScope.first;
            datajson.itemId=$scope.ctrlScope.itemId;
            var fistreleaseTime='';
            var fistdate=$filter('date')($scope.ctrlScope.first1.releaseTime,'yyyy-MM-dd');
            var firsthour=$scope.ctrlScope.first1.hour;
            var firstmin=$scope.ctrlScope.first1.min;
            fistreleaseTime=fistdate+' '+firsthour+':'+firstmin+':'+'00';
            datajson.releaseTime=fistreleaseTime;
            datajson.countdown=0;
            console.info(datajson);
            $http({
                method: 'POST',
                url: "/admin/item/firstVerify",
                data: datajson
            }).success(function(data) {
                console.log(data)
                if(data.result=="success"){
                    toastr.success("提交成功");
                    $scope.viewItemModal = !$scope.viewItemModal;
                    $scope.orderParams.ajaxParam={item:1};
                    $scope.orderParams.reload();
                    $scope.countItem();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error("提交失败");
                    }

                }
            }).error(function() {
            });
        };



    }])
});